package com.one.dao.impl;

import com.one.dao.userDao;
import com.one.pojo.User;
import com.one.utils.DBUtils;

import javax.servlet.http.Cookie;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.DuplicateFormatFlagsException;

/**
 * @author LJ
 * @create 2022-04-24-15:30
 */
public class userDaoImpl implements userDao {
    public static int id;
    @Override
    public boolean userLogin(User user) {
        try (Connection connection = DBUtils.getConnection()){
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name,password FROM user WHERE name=? and password=? ");
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getPassword());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                id=resultSet.getInt("id");
               return true;
            }else {
               return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public int userRegister(User user) {
        try(Connection connection = DBUtils.getConnection();) {
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USER(NAME,PASSWORD) VALUES(?,?) ");
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getPassword());
            int update = preparedStatement.executeUpdate();
            if (update>0){
                id=user.getId();
                return update;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
